package at.guigu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

// 该注解代表该类是SpringMVC的核心配置类
@Configuration
// 配置注解的组件扫描<context:component-scan base-package="at.guigu.controller"></context:component-scan>
// 加载controller对应的bean
@ComponentScan("at.guigu.controller")
// 自动配置 Spring MVC 的各种特性,比如：类型转换器、mvc的注解驱动<mvc:annotation-driven/>、静态资源路径映射
@EnableWebMvc
// 引入配置静态资源类
@Import(SpringMvcSupport.class)
public class SpringMvcConfiguration {
    // 配置视图解析器
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/user/");  // 设置视图文件路径前缀
        resolver.setSuffix(".jsp");   // 设置视图文件后缀
        return resolver;
    }

    // 配置文件上传解析器
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setDefaultEncoding("UTF-8"); // 所上传文件的编码类型
        resolver.setMaxUploadSizePerFile(500000);// 所上传的单个文件的大小
        resolver.setMaxUploadSize(5000000);// 所上传的总文件的大小
        return resolver;
    }
}